//
//  VideoViewController.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/1.
//  Copyright © 2017年 maopu. All rights reserved.
//  猫眼视频首页

import UIKit

class VideoViewController: BaseViewController , UITableViewDelegate , UITableViewDataSource , UIGestureRecognizerDelegate , DoubleLabellDelegate{
    
    fileprivate var tableView:UITableView!
    fileprivate var strList:[String] = ["不要因孤独就找不适合自己的娱乐方式，迎合不属于自己的群体，爱一些就手可得的人。每个人都有孤独时，很多人并不是你印象中的纸醉金迷玩世不恭，他们不为人知的孤独你没看到罢了。不要因一时空虚打乱了你的坚持你的思想，我们都一样，要学会承受人生必然的孤独。过了，才能看见美好繁华。心善之人敢直言，嘴甜之人藏谜奸。宁交一帮抬杠的鬼，不结一群嘴甜的贼！" , "且行且珍惜!"  , "今天天气不太好哦 。。。" , "12345abcd且行且珍惜!12345abcd且行且珍惜!12345abcd且行且珍惜!12345abcd且行且珍惜!12345abcd且行且珍惜!"]
    fileprivate var cellHeightList:[CGFloat] = []
    fileprivate var videoList:[Video] = []

    
    fileprivate var url = "http://baobab.wdjcdn.com/1457162012752491010143.mp4"
    fileprivate var didType = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.initData()
        self.initView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: false)
    }
    
    private func initView(){
        self.navigationItem.title = "猫眼看世界"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.setImgBarButton(imgName: "searchBtn_ico", action: #selector(self.toSearchVCClick), target: self)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.setImgBarButton(imgName: "editButton_ico", action: #selector(self.toReleasePostsVCClick), target: self)
        
        self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: AppWidth, height: AppHeight - 50))
        self.tableView.estimatedRowHeight = 300
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        
        self.view.addSubview(self.tableView)
    }
    
    private func initData(){
        let threeLineHeight = StringSizeEngine.calculateStringHeight("aa\nbb\ncc\n", width: AppWidth - 80, font: UIFont.LightDefaultFont(16))
        for str in self.strList{
            let video = Video()
            video.content = str
            video.isShow = false
            self.videoList.append(video)
            
            var strHeight = StringSizeEngine.calculateStringHeight(str, width: AppWidth - 80, font: UIFont.LightDefaultFont(16))
            if strHeight >= threeLineHeight{
                strHeight = threeLineHeight
            }else{
                strHeight = strHeight + 20
            }
            self.cellHeightList.append(strHeight + 165 + 25 + 16 + 50 + 32)
        }
    }
    
    //MARK: 前往搜索页
    @objc fileprivate func toSearchVCClick(){
        let vc = SearchViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    //MARK: 前往发帖页
    @objc fileprivate func toReleasePostsVCClick(){
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.videoList.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.cellHeightList[indexPath.row]
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell:VideoListItemTabCell? = tableView.dequeueReusableCell(withIdentifier: "VideoListItemTabCell") as? VideoListItemTabCell
        if cell == nil{
            cell = VideoListItemTabCell(style: UITableViewCellStyle.default,reuseIdentifier:"VideoListItemTabCell")
        }
        cell!.delegate = self
        cell?.selectionStyle = .none
        cell?.refreshCellData(video:self.videoList[indexPath.row] , url: self.url , indexPathRow: indexPath.row)
        return cell!
        
    }
    
    func doubleLabel(onDoubleCilck label: UILabel , isShow:Bool , contentHeight:CGFloat) {
        let indexPath = IndexPath.init(row: label.tag, section: 0)
        let video = self.videoList[label.tag]
        video.isShow = isShow
        self.cellHeightList[label.tag] = contentHeight + 165 + 25 + 16 + 50 + 50
        self.tableView.reloadRows(at: [indexPath], with: .automatic)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = VideoReplyDetailController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    
}



